package com.huitone.gddw.controller.monitoring.sms;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.Trigger;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.monitoring.sms.TargetTriggerDTO;
import com.huitone.gddw.service.monitoring.base.ITriggerService;
import com.huitone.gddw.service.monitoring.sms.ITargetTriggerService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 指标监控配置
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="targetTrigger")
public class TargetTriggerController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(TargetTriggerController.class);
	
	@Autowired
	private ITargetTriggerService targetTriggerService;
	
	@Autowired
	private ITriggerService triggerService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<TargetTriggerDTO> query(QueryPage<Trigger> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询指标监控配置列表";
		QueryPage<TargetTriggerDTO> result = null;
		try {
			page.setParams(getParams(request));
			result = targetTriggerService.selectPage(page);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return result;
		} catch (Exception e) {
			LOGGER.error("查询指标监控配置信息出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return result;
		}
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String keyNameCn = request.getParameter("keyNameCn");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("keyNameCn", keyNameCn);
		return params;
	}
	
	/**
	 * 准备修改页面的信息
	 * @param triggerId 触发器ID
	 * @return TargetTriggerDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public TargetTriggerDTO toUpdate(String triggerId) {
		try {
			return targetTriggerService.findOne(triggerId);
		} catch (Exception e) {
			LOGGER.error("指标监控配置--准备修改页面信息时出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 保存
	 * @param entity Trigger
	 * @param request
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(Trigger entity, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加指标监控配置信息";
		try {
			triggerService.save(entity);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("保存指标监控配置信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "保存失败！");
		}
	}
	
	/**
	 * 修改
	 * @param entity Trigger
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(Trigger entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改指标监控配置信息";
		try {
			Trigger trigger = triggerService.getById(entity.getTriggerId());
			trigger.setTriggerName(entity.getTriggerName());
			trigger.setSustainTime(entity.getSustainTime());
			trigger.setExpressions(entity.getExpressions());
			trigger.setIndexDesc(entity.getIndexDesc());
			triggerService.update(trigger);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "修改成功！");
		} catch (Exception e) {
			LOGGER.error("修改指标监控配置信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "修改失败！");
		}
	}
	
}
